package com.ap.core.business.message.domain.translators.impl;

import com.ap.core.backoffice.domain.BackOfficeOrderPayment;
import com.ap.core.business.message.domain.translators.BindingListener;
import com.ap.core.business.message.domain.translators.BindingMetaData;
import com.ap.core.business.message.domain.translators.Translator;
import com.ap.core.business.message.transaction.service.APTransactionGenerationService;
import com.ap.core.common.types.RefundReasonType;
import com.ap.core.dao.DistributorDao;
import com.ap.core.dao.FinancialInstitutionDao;
import com.ap.core.dao.MerchantDao;
import com.ap.core.dao.OrderPaymentRefundDao;
import com.ap.core.domain.*;
import com.ap.core.exception.BusinessFailureException;
import com.ap.core.message.managerefund.info.RequestRetrieveRefundAccountInfo;
import com.ap.core.transaction.APTransactionInfo;
import org.joda.time.LocalDateTime;

/**
 * User: Ranjit.Singh
 * Implementation class for {@link com.ap.core.business.message.domain.translators.Translator} to translate RetrieveRTP1Request bound object to OrderPayment domain entity.
 */
public class RetrieveRefundAcctTranslator implements Translator<RequestRetrieveRefundAccountInfo> {

    
    private OrderPaymentRefundDao orderPaymentRefundDao;
    private APTransactionGenerationService apTransactionGenerationService;
    private DistributorDao distributorDao;
    private MerchantDao merchantDao;
    private FinancialInstitutionDao financialInstitutionDao;

  	@Override
	public void translate(RequestRetrieveRefundAccountInfo boundObject,
			BindingMetaData metaData, BindingListener bindingListener)
			throws BusinessFailureException {
		
		   BackOfficeOrderPayment originalOrderPayment = metaData.getBackOfficeOrderPayment();
		   final APTransactionInfo apTransaction = apTransactionGenerationService.generateApTransaction(metaData.getReconciliationInfo().getReconciliationDate());
	       final String refundApTransactionId = apTransaction.getApTransactionId();
	       final Long refundRetrievalId = Long.valueOf(apTransaction.getApTransactionRetrievalId());
	       final Long distributorId = distributorDao.getDistributorByIdentification(boundObject.getRefundRequestInfo().getDistributorId()).getId();
	       final Long merchantId = merchantDao.getMerchantByIdentification(boundObject.getRefundRequestInfo().getMerchantId()).getId();
	       
	       OrderPaymentRefund orderPaymentRefund = new OrderPaymentRefund(metaData.getApMessageId(), refundApTransactionId,refundRetrievalId,
		   originalOrderPayment.getId(), originalOrderPayment.getApTransactionRetrievalId(), originalOrderPayment.getCurrencyCode(),
		   metaData.getReconciliationInfo().getReconciliationCycleId(), metaData.getReconciliationInfo().getReconciliationDate(), distributorId,
		   merchantId, Amount.valueOf(boundObject.getRefundRequestInfo().getRefundAmount()), 
		   boundObject.getRefundRequestInfo().getRefundMethod(), boundObject.getRefundRequestInfo().getRefundReasonType(), new LocalDateTime(), new LocalDateTime());
	       
	       if(boundObject.getRefundRequestInfo().getRefundReasonType().equals(RefundReasonType.DISPUTES.name())){
	    	   FinancialInstitution financialInstitution = financialInstitutionDao.findById(originalOrderPayment.getFiId());
	    	    orderPaymentRefund.setAccountNumber(AccountNumber.valueOf(financialInstitution.getHoldingAcctNumber()));
	    	    if(financialInstitution.getHoldingSortCode() != null){
	    	    	orderPaymentRefund.setSortCode(SortCode.valueOf(financialInstitution.getHoldingSortCode()));
	    	    }
	 		    if(financialInstitution.getHoldingRollNumber() != null){
	 			   orderPaymentRefund.setRollNumber(RollNumber.valueOf(financialInstitution.getHoldingRollNumber()));
	    	    }
	       }else{
	    	   orderPaymentRefund.setAccountNumber(originalOrderPayment.getDebitAccountNumber());
	    	   if(originalOrderPayment.getDebitSortCode() != null){
	    		   orderPaymentRefund.setSortCode(originalOrderPayment.getDebitSortCode());
	    	    }
	 		    if(originalOrderPayment.getRollNumber() != null){
	 		    	  orderPaymentRefund.setRollNumber(originalOrderPayment.getRollNumber());
	    	    }
	       }
	       
		   orderPaymentRefund.setFiId(originalOrderPayment.getFiId());
		   orderPaymentRefund.setFiCustomerId(originalOrderPayment.getFiCustomerId());
		   orderPaymentRefund.setZappConsumerId(originalOrderPayment.getZappConsumerId());
		  
		   orderPaymentRefund.setMerchantRefundRef(boundObject.getRefundRequestInfo().getMerchantRefundRef());
		   orderPaymentRefundDao.persist(orderPaymentRefund);
		   bindingListener.onOrderPaymentRefund(orderPaymentRefund, originalOrderPayment);
	}
	
	
	 public void setorderPaymentRefundDao(OrderPaymentRefundDao orderPaymentRefundDao) {
	        this.orderPaymentRefundDao = orderPaymentRefundDao;
	    }

	public void setApTransactionGenerationService(APTransactionGenerationService apTransactionGenerationService) {
		this.apTransactionGenerationService = apTransactionGenerationService;
	}
	
	public void setDistributorDao(DistributorDao distributorDao) {
		this.distributorDao = distributorDao;
	}

	public void setMerchantDao(MerchantDao merchantDao) {
		this.merchantDao = merchantDao;
	}


	public void setFinancialInstitutionDao(
			FinancialInstitutionDao financialInstitutionDao) {
		this.financialInstitutionDao = financialInstitutionDao;
	}
}
